/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slideshow;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author Filippo
 */
public class ImagePanel extends JPanel {
    private BufferedImage image;
    private int x;
    private int y;
    public ImagePanel() {
        this.x = 0;
        this.y = 0;
    }
    public BufferedImage getImage() {
        return this.image;
    }
    public void setImage(BufferedImage image) {
        this.image = image;
        this.x = this.adaptX(image);
        this.y = this.adaptY(image);
        this.repaint();
    }
    private int adaptX(BufferedImage image) {
        int x = 0;
        double width = image.getWidth();
        double widthScale = width / WindowConstants.IMAGE_WIDTH;
        if(widthScale > 0 && widthScale < 1) {            
            x = (int)((WindowConstants.IMAGE_WIDTH - width) / 2);
        }
        return x;
    }
    private int adaptY(BufferedImage image) {
        int y = 0;
        double height = image.getHeight();
        double heightScale = height / WindowConstants.IMAGE_HEIGHT;
        if(heightScale > 0 && heightScale < 1) {            
            y = (int)((WindowConstants.IMAGE_HEIGHT - height) / 2);
        }
        return y;
    }
    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(this.image,this.x,this.y,this);
    }
}
